// vp_cai/pages/index/citys.js
var app = getApp();
var vp = require('../../resource/js/vp.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isClientAdd: false,
    index: 0,
    client: {}, // 新增模型
    mobile:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this);
    var self = this;
    let ages = [];
    for (let index = 0; index < 100; index++) {
      ages.push(index + 1);
    }

    self.setData({
      act: options.act ? options.act : '',
      ages
    });

    if (options.act == 'select') {
      wx.setNavigationBarTitle({
        title: '请选择手机号',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '手机号管理',
      })
    }

    app.util.getUserInfo(function (response) {
      self.setData({
        userInfo: response
      });
      app.mdInit(function (cfg, mine, area) {
        self.setData({
          cfg: cfg,
          mine: mine,
          area: area
        });

        self.main_load(false);
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getMobile: function (e) {
    let self= this;
    console.log(e.detail.code)
    wx.login({
      success: function (res) {
        console.log(res);
        e.detail.code=res.code
        app.util.getMobile(function (data) {
          console.log('getMobile',data.data.data.phoneNumber);
          console.log(data);
          self.setData({
            mobile:data.data.data.phoneNumber
          })
        }, e.detail)
      },
      fail: function () {
        wx.showModal({
          title: '获取信息失败',
          content: '请允许授权以便为您提供给服务',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    });
    
  },
  // 页面数据加载
  main_load(showLoading) {
    const self = this;
    //console.log("self.data.like "+self.data.like);
    app.util.request({
      url: 'User/mobiles',
      showLoading: showLoading ? true : false,
      data: {
        m: app.mdName,
        aid: self.data.area.id
      },
      success(res) {
        var data = res.data.data;
        self.setData({
          main_loaded: true,
          now: data.now,
          clients: data.clients,
          isClientAdd: ((data.clients && data.clients.length > 0) ? false : true)
        });
      }
    })
  },

  onClientSelected: function (e) {
    if (this.data.act == '') {
      return;
    }
    var index = e.currentTarget.dataset.index
    var client = this.data.clients[index];

    let pages = getCurrentPages();
    if (pages.length >= 2) {
      var prevPage = pages[pages.length - 2]; //上一个页面
      //给pageA页面赋值
      prevPage.setData({
        mobile: client
      });

      wx.navigateBack();
    }
  },

  removeClient: function (e) {
    var self = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定移除该手机号？',
      success(res) {
        if (res.confirm) {
          app.util.request({
            url: 'User/usermobile',
            data: {
              m: app.mdName,
              aid: self.data.area.id,
              submit: 'remove',
              id: id
            },
            success(res) {
              wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000,
                success: function () {
                  setTimeout(function () {
                    self.main_load(true);
                  }, 1800);
                }
              });
            }
          });
        }
      }
    });
  },
  switchClientAdd: function (e) {
    this.setData({
      isClientAdd: !this.data.isClientAdd
    });
  },
  clientSave: function (e) {
    var self = this;
    if (self.data._is_saving) {
      return;
    }
    var client = this.data.client;

    if (!e.detail.value.mobile) {
      return wx.showToast({
        title: '请填手机号',
        icon: 'none',
        duration: 2000
      });
    }
    client.mobile = e.detail.value.mobile;
    self.setData({
      mobile:''
    })
    // 序列化提交数据对象
    var form = encodeURI(JSON.stringify(client));
    self.setData({
      _is_saving: true
    });
    app.util.request({
      url: 'User/usermobile',
      data: {
        m: app.mdName,
        aid: self.data.area.id,
        submit: 'save',
        form: form
      },
      complete(res) {
        self.setData({
          _is_saving: false
        });
      },
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              self.main_load();
            }, 2000);
          }
        });
      }
    })
  },
})